package com.lovo.test;

import java.util.Arrays;
import java.util.InputMismatchException;
import java.util.Scanner;

public class GuessNumber {
    public static void main(String[] args) {
        int[] computerArr = getnumber();
        for (int i = 0; i < 5; i++) {
            int[] luckyArr = whitenumber();
            boolean d = pan(computerArr, luckyArr);
            if (d) {
                break;
            }
        }
        System.out.println("正确答案是：" + Arrays.toString(computerArr));

    }

    //判断
    public static boolean pan(int[] computerArr, int[] luckyArr) {
        int a = 0;
        int b = 0;
        for (int i = 0; i < computerArr.length; i++) {
            for (int j = 0; j < luckyArr.length; j++) {
                if (computerArr[i] == luckyArr[j] && i == j) {
                    a++;
                }
                if (computerArr[i] == luckyArr[j] && i != j) {
                    b++;
                }
            }
        }
        System.out.println(a + "A" + b + "B");
        if (a == 4) {
            System.out.println("恭喜你答对了");
            return true;
        }
        return false;
    }

    //让用户输入四次的数组
    public static int[] whitenumber() {
        boolean log = false;
        int[] arr = new int[4];
        for (int i = 0; i < arr.length; i++) {
            //异常
            try {
                Scanner scan = new Scanner(System.in);
                System.out.println("请输入一个0~9的数字" + i);
                int number = scan.nextInt();
                for (int j = 0; j < i; j++) {
                    if (arr[j] == number) {
                        System.out.println("重复输入的相同的数，重新输入");
                        log = true;
                        i--;
                        break;
                    }
                }
                if (!log) {
                    arr[i] = number;
                }
            } catch (InputMismatchException e) {
                System.out.println("输入无效，请重新输入");
                i--;
            }


        }
        System.out.println(Arrays.toString(arr));
        return arr;
    }

    //生成四次的随机数组
    public static int[] getnumber() {
        int[] arr = new int[4];
        for (int i = 0; i < 4; i++) {
            int number = random(0, 9);
            boolean flag = false;
            for (int j = 0; j < i; j++) {
                if (arr[j] == number) {
                    flag = true;
                    i--;
                    break;
                }
            }
            if (!flag) {
                arr[i] = number;
            }
        }
        return arr;
    }

    // 随机数
    public static int random(int min, int max) {
        //生成随机数 min max
        return (int) (Math.random() * (max + 1 - min) + min);
    }
}


